import headImg from '@/assets/images/head-img.jpg'
import { getSid, setInfo, getInfo} from '@/utils/storage'
import{doLogout, updateLoginPwd} from '@/api/login'
import router from '@/router'
import {MessageBox} from 'element-ui'

export default {
    namespaced: true,
    state() {
        return {
            avatar: headImg,
            userInfo: getInfo()
        }
    },
    mutations: {
        setUserInfo(state, loginName) {
            let userInfo = {sid: getSid(), loginName: loginName}
            state.userInfo = userInfo
            setInfo(userInfo)
        },
        clearUserInfo(state) {
            state.userInfo = {}
            setInfo({})
        }
    },
    actions: {
        async logoutAct(context) {
            await doLogout()
            context.commit('clearUserInfo')
            router.replace('/login')
        },
        async updateLoginPwdAct(context, password) {
            await updateLoginPwd(password)
            context.commit('clearUserInfo')
            MessageBox.alert('密码修改成功,请重新登录', '提示', {
                showClose: false,
                type: 'success',
                callback: action => {
                    router.replace('/login')
                }
            })
        }
    },
    getters: {

    }
}